<?php
$environment = 1;
?>
<div class="container" id="install-body">
    <div class="row">
        <div class="col-md-3">
            <div class="card border-primary mb-3">
                <div class="card-header">安装步骤</div>
                <div class="list-group list-group-flush">
                    <a href="#" class="list-group-item list-group-item-action ">
                        1.同意注册协议
                    </a>
                    <a href="#" class="list-group-item list-group-item-action ">
                        2.安装环境检测
                    </a>
                    <a href="#" class="list-group-item list-group-item-action active">
                        3.设置基础信息
                    </a>
                    <a href="#" class="list-group-item list-group-item-action ">
                        4.进行安装
                    </a>
                    <a href="#" class="list-group-item list-group-item-action ">
                        5.安装完成
                    </a>
                </div>
            </div>
        </div>
        <div class="col-md-9">
            <form id="form-database" action="index.php?r=install/default/install" method="post"  >
                <input type="hidden" name="YII_CSRF_TOKEN" value="<?php echo Yii::app()->request->csrfToken; ?>"/>
                <div class="card border-primary mb-3">
                    <div class="card-header">数据库配置</div>
                    <div class="card-body">
                        <fieldset>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group ">
                                        <label>数据库IP</label>
                                        <input type="text" class="form-control validate[required]" value="" name="host" placeholder="数据库IP">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>数据库端口</label>
                                        <input type="text" class="form-control validate[required]" value="" name="port" placeholder="数据库端口">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>数据库名称</label>
                                        <input type="text" class="form-control validate[required]" value="" name="database" placeholder="数据库名称">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>用户名</label>
                                        <input type="text" class="form-control validate[required]" value="" name="username" placeholder="用户名">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>密码</label>
                                        <input type="text" class="form-control validate[required]" value="" name="password" placeholder="密码">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                </div>
                            </div>
                        </fieldset>

                        <div class="form-group " id="alert-database" style="display: none;">
                            <div class="alert alert-dismissible alert-warning">
                                <button type="button" class="close" data-dismiss="alert">&times;</button>
                                <p class="mb-0 message">
                                </p>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="card border-primary mb-3">
                    <div class="card-header ">用户设置</div>
                    <div class="card-body">
                        <fieldset>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>管理员名称</label>
                                        <input type="text" class="form-control validate[required]" value="" name="manager" placeholder="管理员名称">
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label>密码</label>
                                        <input type="text" class="form-control validate[required]" value="" name="loginpass" placeholder="密码">
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                </div>
            </form>
            <form class="mb-3">
                <?php
                if(isset($this->data['message'])){
                    ?>
                    <div class="form-group">
                        <div class="alert alert-dismissible alert-warning">
                            <button type="button" class="close" data-dismiss="alert">&times;</button>
                            <p class="mb-0">
                                <?php echo $this->data['message']; ?>
                            </p>
                        </div>
                    </div>
                    <?php
                }
                ?>
                <div class="form-group">
                    <button type="button" class="btn btn-primary btn-block btn-lg" onclick="nextStep()">下一步</button>
                </div>
            </form>

        </div>
    </div>
</div>
<script>
    $(function () {
        $("#form-database").validationEngine();
        changeposition("inline");
    });
    function nextStep() {
        if(checkParams()){
            checkDatabase();
        }
    }
    function checkParams() {
        return $("#form-database").validationEngine('validate')
    }
    function checkDatabase() {
        var params = {
            host:$("#form-database").find("input[name=host]").val(),
            port:$("#form-database").find("input[name=port]").val(),
            database:$("#form-database").find("input[name=database]").val(),
            username:$("#form-database").find("input[name=username]").val(),
            password:$("#form-database").find("input[name=password]").val(),
            YII_CSRF_TOKEN:'<?php echo Yii::app()->request->csrfToken; ?>'
        };
        $.post('index.php?r=install/default/testdatabase',params,function (data) {
            if(data.code != 10000){
                $("#alert-database .message").text("数据库连接异常");
                $("#alert-database").show();
            }else{
                $("#form-database").submit();
                $("#alert-database").hide();
            }
        },'json');
    }
    function changeposition(wo) {
        $('#form-database').validationEngine('hide');
        $('input').attr('data-prompt-position',wo);
        $('input').data('promptPosition',wo);
        $('textarea').attr('data-prompt-position',wo);
        $('textarea').data('promptPosition',wo);
        $('select').attr('data-prompt-position',wo);
        $('select').data('promptPosition',wo);
    }
</script>